package exam;

import java.util.Scanner;
public class meituan {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String t = in.nextLine();
        String s = in.nextLine();
        int sc = 0;
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)=='C') sc++;
        }
        int sj = s.length()-sc;
        if(sc-sj==1){ //c多
            int res = 0;
            for(int i =0;i<s.length();i++){
                if(i%2==0){
                    if(s.charAt(i)=='C') continue;
                    else{
                        int start=i;
                        while(s.charAt(start)!='C') start++;

                        res+=start-i;
                    }
                }
            }
            System.out.println(res);
        }else if(sc-sj==-1){
            int res = 0;
            for(int i =0;i<s.length();i++){
                if(i%2==0){
                    if(s.charAt(i)=='J') continue;
                    else{
                        int start=i;
                        while(s.charAt(start)!='J') start++;
                        res+=start-i;
                    }
                }
            }
            System.out.println(res);

        }else if(sc==sj){
            int res = 0;
            for(int i =0;i<s.length();i++){
                if(i%2==0){
                    if(s.charAt(i)=='C') continue;
                    else{
                        int start=i;
                        while(s.charAt(start)!='C') start++;
                        res+=start-i;
                    }
                }
            }
            int res2 =0;
            for(int i =0;i<s.length();i++){
                if(i%2==0){
                    if(s.charAt(i)=='J') continue;
                    else{
                        int start=i;
                        while(s.charAt(start)!='J') start++;
                        res2+=start-i;
                    }
                }
            }
            System.out.println(res>res2?res2:res);
        }else{
            System.out.println(-1);
        }

////        2
////        4 4
////        5 5 3 3
////        1 1 5 3
////        2 2 5 4
////        2 2 4 4
////        3 4
////        1 1 1 1
////        2 2 3 1
////        2 2 1 1
//        Scanner in = new Scanner(System.in);
//        String t = in.nextLine();
//        String s = in.nextLine();
//        String t2 = in.nextLine();
//        String s2 = in.nextLine();
//        String t3 = in.nextLine();
//        String s3 = in.nextLine();
//        String t4 = in.nextLine();
//        String s4 = in.nextLine();
//        String t5 = in.nextLine();
//        String s5 = in.nextLine();
//        System.out.println("Yes");
    }
}
